# encoding: utf-8
import json
import logging 
import os
import random
import re
import time
import urllib2

import BeautifulSoup

from src.crawler import util


logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s [%(lineno)d] %(levelname)s %(message)s',
                datefmt='%Y-%m-%d %H:%M:%S',
                filename='app.log',
                filemode='w')
logger = logging.getLogger();

class Baidu(object):
    def __init__(self, copyright_id, song_name, singer_name):
        self.copyright_id = copyright_id;
        self.song_name = song_name;
        self.singer_name = singer_name;
        
        self.baidu_song_id = '';
        self.baidu_song_name = '';
        self.baidu_singer_name = '';
        self.baidu_lrc_url = '';
        self.baidu_redirect_mp3_url = ''
        self.baidu_mp3_url = '';
    
    def __str__(self):
        return str(self.copyright_id) + '|' + self.song_name + '|' + self.singer_name + '|' + str(self.baidu_song_id) + '|' + self.baidu_song_name + '|' + self.baidu_singer_name + '|' + str(self.baidu_lrc_url) + '|' + str(self.baidu_mp3_url) 
        
    
#     搜索入口，关键：song_name singer_name
    def search(self):
        url = 'http://sug.music.baidu.com/info/suggestion?format=json&word=' + urllib2.quote(self.song_name + ' ' + self.singer_name) + '&version=2&from=0&callback=window.baidu.sug&_=' + str(time.time()) + str(random.randint(0, 9))
        rsp = urllib2.urlopen(url)
        html = rsp.read()
        #   html='window.baidu.sug({"data":{"song":[{"songid":"1102942","songname":"\u5341\u5e74","artistname":"\u9648\u5955\u8fc5"}],"artist":[],"album":[]},"Pro":["song"]});';
        json_str = self.extract_json(html)
        if (json_str != '') :
            jo = json.loads(json_str)
            if len(jo['data']['song']) > 0:
                jsong = jo['data']['song'][0]
                self.baidu_song_id = jsong['songid']
                self.baidu_song_name = jsong['songname'].encode('utf-8')
                self.baidu_singer_name = jsong['artistname'].encode('utf-8')
#                 song_index(song_id)
#                 mp3_download_page(song_id)
#     提取搜索返回json中的songid
    def extract_json(self, rsp):
        pattern = re.compile('window.baidu.sug\((.*)\)');
        match = pattern.match(rsp);
        ret = '';
        if match:
            ret = match.group(1);
        return ret;
    
#     歌曲主页
    def song_index_url_format(self):
        if len(self.baidu_song_id) > 0 :
            url = 'http://music.baidu.com/song/' + str(self.baidu_song_id) + '?pst=sug'
            rsp = urllib2.urlopen(url)
            html = rsp.read();
            if len(html) > 0:
                self.parse_song_index(html);
        else:
            logger.info('song_index : ' + str(self.copyright_id));
            
#     解析歌曲主页    
    def parse_song_index(self, html):
        soup = BeautifulSoup.BeautifulSoup(html)
        hrefs = soup.findAll(attrs={'class':'down-lrc-btn'});
#   [<a class="down-lrc-btn" data-lyricdata='{ "href":"/data2/lrc/13767709/%E5%8D%81%E5%B9%B4.lrc" }' href="#">下载LRC歌词</a>]
        if len(hrefs) > 0:
            json_str = hrefs[0]['data-lyricdata'];
            jo = json.loads(json_str);
            href = jo['href'];
            if len(href) > 0:
                url = 'http://music.baidu.com' + href;
                self.baidu_lrc_url = url
        surls = soup.findAll(attrs={'class':'link-src-info'});
        if len(surls) > 0:
            for surl in surls:
                ahref = surl.find('a')
                self.baidu_redirect_mp3_url = ahref['href']
#     歌词下载            
    def lrc_download_handler(self):
        if len(self.baidu_lrc_url) > 0:
            filename = 'lrc/' + self.copyright_id + ".lrc";
            if util.checkfile(filename) == True:
                return 
            rsp = urllib2.urlopen(self.baidu_lrc_url)
            html = rsp.read();
            f = open (filename, "w");
            f.writelines(html);
            f.close();
            logger.info('lrc downloaded : ' + self.baidu_lrc_url)
        else:
            logger.info('lrc download faile : ' + str(self.baidu_song_id))
            
        
#    歌曲下载主页 
    def mp3_download_page(self):
        filename = 'mp3/' + self.copyright_id + ".mp3";
        if util.checkfile(filename) == True :
            return
        if len(self.baidu_redirect_mp3_url) > 0 :
            self.redirect_mp3_download_page();
            return
        header = {
                  'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    #     'Accept-Encoding':'gzip,deflate,sdch',
                  'Accept-Language':'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
                  'Cache-Control':'max-age=0',
                  'Connection':'keep-alive',
                  'Cookie':'BAIDUID=8F0AD02D635A01A7E463952A41BC308B:FG=1; BDRCVFR[T77Y7lc0xct]=rJZwba6_rOCfAF9pywd; LUP=bt%3D1397355264%26email%3Dicandiio%2540hotmail.com%26f%3D1%26loginname%3Dicandiio%2540hotmail.com%26mobile%3D%26nick%3DJustin%25C1%25F7%26nickname%3D%25E6%25B5%25AA%25E8%25BF%25B9%25E6%25B5%2581%26uid%3D2184607024%26user%3Dicandiio%2540hotmail.com%26ut%3D2014-03-11%2B15%253A48%253A59; LUE=5a05ca34a19cbb2ed23dbc1c6e78adee; LUE2=iQmunLtBTaOgI6ltnvxC8BcuoFcKQWz1IbbW2s7ZyyJQja6R1%2FvqupDXPbfDZv0dIZGP7XUv8TqF%2FjANB2NcEXPUhDiBcOexLVdmK1pLTyXHmPlKCRiz83U38Px2cBquuNs9WlyZRMSjhJrpy5gTKjZBjxv2PM2bVh862rTTz64DUEPCiPhsSAChucz5AwNu8mjiaHYtnGjGJfhQ7hGtmC64Q3L7ai3moxTQh1pEKMLySjbF7zhKeTkeZ%2BBH3%2Ft25b4tjghT43qs774b85kZzsqQSvHMmw5EcUEAOy9GNwC0Iw4%2B5ygeJ6aTZkdI%2F9IORdQkcPkD6D3LtRRZwxMedie5FelHSVptPj0GvJ28zshtcUFpsKWI%2B3fXhajRpL0E; NBID=F8CEE2A8620E0FFE0BF134408A9266E4:FG=1; BDREFER=%7Burl%3A%22http%3A//finance.baidu.com/%22%2Cword%3A%22%22%7D; locale=zh; bdshare_firstime=1398157612047; cflag=65535:1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BDUSS=loxWlZaMFZ0U3VTcndzU35NQjdTSklDeDdOfnFSMmg0WH5VVS1OWlVVa3JPSUZUQVFBQUFBJCQAAAAAAAAAAAEAAADipNgfaWNhbmRpaW8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACurWVMrq1lTW; H_PS_PSSID=4684_6043_1454_5223_6023_4759_6018_6257_5856_6191; Hm_lvt_d0ad46e4afeacf34cd12de4c9b553aa6=1398338879,1398349356,1398350333,1398385486; Hm_lpvt_d0ad46e4afeacf34cd12de4c9b553aa6=1398385486; tracesrc=-1%7C%7Cwww.baidu.com; u_lo=0; u_id=; u_t=',
                  'Host':'music.baidu.com',
                  'Referer':'http://music.baidu.com/song/1102942?pst=sug',
                  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36'
        }
        reqs = urllib2.Request(url='http://music.baidu.com/song/' + str(self.baidu_song_id) + '/download?__o=%2Fsong%2F' + str(self.baidu_song_id) , headers=header)
        html = urllib2.urlopen(reqs).read();
    
        soup = BeautifulSoup.BeautifulSoup(html)
        hrefs = soup.findAll(attrs={'id':'128'});
    #   [<a class="down-lrc-btn" data-lyricdata='{ "href":"/data2/lrc/13767709/%E5%8D%81%E5%B9%B4.lrc" }' href="#">下载LRC歌词</a>]
        print hrefs
        if len(hrefs) > 0:
            href = hrefs[0]['href']
        # str = '/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/31629377/1102942165600128.mp3?xcode=d1a68c7d95b09527dcb4000479b665c2e095a677f06688ee&song_id=1102942'
            index = href.find('http');
            if index > 0 :
                url = href[index:];
                print url;
                self.baidu_mp3_url = url
    def redirect_mp3_download_page(self):
        print 'error'
        
        
#     下载歌曲
    def mp3_download_handler(self):
        if len(self.baidu_mp3_url) > 0:
            header = {
                  'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    #     'Accept-Encoding':'gzip,deflate,sdch',
                  'Accept-Language':'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
                  'Cache-Control':'max-age=0',
                  'Connection':'keep-alive',
                  'Cookie':'8F0AD02D635A01A7E463952A41BC308B:FG=1; BDRCVFR[T77Y7lc0xct]=rJZwba6_rOCfAF9pywd; PANWEB=1; bdshare_firstime=1397091438558; LUP=bt%3D1397355264%26email%3Dicandiio%2540hotmail.com%26f%3D1%26loginname%3Dicandiio%2540hotmail.com%26mobile%3D%26nick%3DJustin%25C1%25F7%26nickname%3D%25E6%25B5%25AA%25E8%25BF%25B9%25E6%25B5%2581%26uid%3D2184607024%26user%3Dicandiio%2540hotmail.com%26ut%3D2014-03-11%2B15%253A48%253A59; LUE=5a05ca34a19cbb2ed23dbc1c6e78adee; LUE2=iQmunLtBTaOgI6ltnvxC8BcuoFcKQWz1IbbW2s7ZyyJQja6R1%2FvqupDXPbfDZv0dIZGP7XUv8TqF%2FjANB2NcEXPUhDiBcOexLVdmK1pLTyXHmPlKCRiz83U38Px2cBquuNs9WlyZRMSjhJrpy5gTKjZBjxv2PM2bVh862rTTz64DUEPCiPhsSAChucz5AwNu8mjiaHYtnGjGJfhQ7hGtmC64Q3L7ai3moxTQh1pEKMLySjbF7zhKeTkeZ%2BBH3%2Ft25b4tjghT43qs774b85kZzsqQSvHMmw5EcUEAOy9GNwC0Iw4%2B5ygeJ6aTZkdI%2F9IORdQkcPkD6D3LtRRZwxMedie5FelHSVptPj0GvJ28zshtcUFpsKWI%2B3fXhajRpL0E; NBID=F8CEE2A8620E0FFE0BF134408A9266E4:FG=1; BDREFER=%7Burl%3A%22http%3A//finance.baidu.com/%22%2Cword%3A%22%22%7D; locale=zh; t_pcnt=20140425-0; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=4684_6043_1454_5223_6023_4759_6018_6257_5856_6191; BDUSS=5Ed3BtOEgwMlY1MEpOSFRaaXdjS24xRkYxYnBuSUZzMEdPY0lzbWJiLU1tWUZUQVFBQUFBJCQAAAAAAAAAAAEAAAClmMtFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIwMWlOMDFpTTX; Hm_lvt_773fea2ac036979ebb5fcc768d8beb67=1398394562,1398394734,1398394971,1398410394; Hm_lpvt_773fea2ac036979ebb5fcc768d8beb67=1398410394; Hm_lvt_adf736c22cd6bcc36a1d27e5af30949e=1398394562,1398394736,1398394971,1398410394; Hm_lpvt_adf736c22cd6bcc36a1d27e5af30949e=1398410394; recommendTime=guanjia2014-04-23 11:52; cflag=65535:1; livingTag=1398410392850',
#                   'Host':'music.baidu.com',
#                   'Referer':'http://music.baidu.com/song/1102942?pst=sug',
                  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36'
            }
            f = None;
            if self.baidu_mp3_url.index('http://nj') > -1:
                req = urllib2.Request(url=self.baidu_mp3_url, headers=header)
                f = urllib2.urlopen(req)
            else :
                f = urllib2.urlopen(self.baidu_mp3_url) 
            with open('mp3/' + str(self.copyright_id) + ".mp3", "wb") as code:
                code.write(f.read())
            logger.info('map3 downloaded : ' + self.baidu_mp3_url)
        else:
            logger.info('map3 downloaded fail : ' + str(self.baidu_song_id))

class QQ(object):
    search_url_format = 'http://s.plcloud.music.qq.com/fcgi-bin/smartbox.fcg?o_utf8=1&utf8=1&key={keyword}&g_tk=938407465&loginUin=0&hostUin=0&format=jsonp&inCharset=GB2312&outCharset=utf-8&notice=0&platform=yqq&jsonpCallback=MusicJsonCallback&needNewCode=0'
    lrc_url = 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=(mid)'
    key_url = 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid={guid}&g_tk=938407465&loginUin=0&hostUin=0&format=jsonp&inCharset=GB2312&outCharset=GB2312&notice=0&platform=yqq&jsonpCallback=MusicJsonCallback&needNewCode=0'
    mp3_url = 'http://cc.stream.qqmusic.qq.com/C200{mid}.m4a?vkey={key}&&guid={guid}&fromtag=0'
    def __init__(self, copyright_id, song_name, singer_name):
        self.copyright_id = copyright_id;
        self.song_name = song_name;
        self.singer_name = singer_name;
        
        self.qq_song_id = '';
        self.qq_song_key = '';
        self.qq_song_name = '';
        self.qq_singer_name = '';
        self.qq_lrc_url = '';
        self.qq_redirect_mp3_url = ''
        self.qq_mp3_url = '';
        
    def __str__(self):
        return str(self.copyright_id) + '|' + self.song_name + '|' + self.singer_name + '|' + str(self.qq_song_id) + '|' + str(self.qq_song_key) + '|' + self.qq_song_name + '|' + self.qq_singer_name + '|' + str(self.qq_lrc_url) + '|' + str(self.qq_mp3_url) 
     
    def search(self):
        url = QQ.search_url_format.format(urllib2.quote(self.song_name + ' ' + self.singer_name))
        print url
        rsp = urllib2.urlopen(url)
        html = rsp.read()
        print html
        #   html='MusicJsonCallBack({retcode:0, tips:{song:[{id:265242, mid:"0017FBUP3syUId", name:"信不信由你", singer_name:"张露"}],singer:[],album:[],mv:[]}});';
        json_str = self.extract_json(html)
        print json_str
        if (json_str != '') :
            jo = json.loads(json_str)
            print len(jo['tips']['song'])
            if len(jo['tips']['song']) > 0:
                jsong = jo['tips']['song'][0]
                self.qq_song_id = jsong['id']
                self.qq_song_key = jsong['mid']
                self.qq_song_name = jsong['name'].encode('utf-8')
                self.qq_singer_name = jsong['singer_name'].encode('utf-8')
                
    def extract_json(self, rsp):
        pattern = re.compile('MusicJsonCallBack\((.*)\)');
        match = pattern.match(rsp);
        ret = '';
        if match:
            ret = match.group(1);
        return ret;
    
#     def lrc_download_handler(self):
#         
#     
#     def mp3_download_handler(self):
        
        

def check_file(filename):
    return os.path.exists(filename)

def csv():
    f = open('D:/Users/usr/Desktop/1.csv', 'r');
    for line in f:
        l = line.split(',')
        baidu = Baidu(l[0], l[1], l[2]);
        try:
            baidu.search();
            baidu.song_index_url_format();
            baidu.mp3_download_page();
            print baidu
            map3_download = False
            if len(baidu.baidu_mp3_url) > 0:
                map3_download = True
            baidu.lrc_download_handler();
            baidu.mp3_download_handler();
            if map3_download :
                time.sleep(30)
        finally:
            logger.info(baidu);
    f.close();
    
# csv()


# baidu = Baidu(123, '夜', '阿朵')
# try:
#     ncr = 'http://nj.baidupcs.com/file/1449a29c09614fb3b657573ad9514b08?fid=2436027485-250528-236942019565954&time=1398412104&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-SApsCZQIJflM%2B7HuPpPxUhPALe8%3D&to=nb&fm=N,B,T,t&newver=1&expires=1398412704&rt=sh&r=535656286&logid=4036252843&sh=1&vuk=2554456852&fn=%E5%A4%9C.mp3'
#     # baidu.baidu_song_id = 1552949
#     # baidu.song_index();
#     baidu.baidu_mp3_url = ncr;
#     baidu.mp3_download_handler();
# finally:
#     print baidu;
    

'http://pan.baidu.com/share/link?shareid=3615288866&uk=2436027485'
